package com.kexun.config;

import com.kexun.common.redis.RedisUtil;
import com.kexun.interceptor.SecurityInterceptor;
import com.kexun.resolver.LoginUserHandlerMethodArgumentResolver;
import com.kexun.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Component
public class WebMvcConfig implements WebMvcConfigurer {

    private final ManageService userService;
    private final RedisUtil redisUtil;

    public WebMvcConfig(ManageService userService, RedisUtil redisUtil) {
        this.userService = userService;
        this.redisUtil = redisUtil;
    }


    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("welcome").setViewName("welcome");
        registry.addViewController("noLogin").setViewName("noLogin");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new LoginUserHandlerMethodArgumentResolver(userService));
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SecurityInterceptor(redisUtil)).addPathPatterns("/**").excludePathPatterns("/layuiadmin/**", "/img/**", "/css/**", "/js/**", "/favicon.ico");
    }
}
